package com.bleeth.fx.helper;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.bleeth.fx.layout.FormBuilder;
import com.bleeth.fx.layout.GirdCell;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.effect.BoxBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author ：Bleeth
 * @date ：2021-11-07
 * @description：组件创建工具
 */
public class AppHelper {


    public static Stage mainStage;

    public static Pane mainPane;


    public static List<Region> messageList = new ArrayList<>();

    public static void init(Stage primaryStage) {
        mainStage = primaryStage;
        mainPane = new Pane();
    }

    public static Region createWindowBar(Region form) {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER_RIGHT);
        Region closeImg = ControlsHelper.createWrapperImg("img/close.png", 30, 40, false);
        hBox.getChildren().add(closeImg);
        FormBuilder girdBuilder = FormBuilder.queryForm(form);
        girdBuilder.setRow(0, GirdCell.createCell(1, Pos.CENTER))
                .coverCell(0, hBox)
                .endRow(false);


        closeImg.setOnMouseClicked(event -> {
            mainStage.close();
        });
        return hBox;
    }


    public static void showForm(String bgUrl, Region form) {
        System.err.println(bgUrl);
        StackPane stackPane = new StackPane();

        ImageView imageView = new ImageView(new Image(URLUtil.url(bgUrl).toExternalForm()));
        BoxBlur bb = new BoxBlur();
        bb.setWidth(5);
        bb.setHeight(5);
        bb.setIterations(3);
        imageView.setEffect(bb);
        imageView.setOpacity(0.6);
        stackPane.getChildren().addAll(imageView, form);
        stackPane.setPrefHeight(form.getPrefHeight());
        stackPane.setPrefWidth(form.getPrefWidth());
        showForm(stackPane);
    }

    public static void showForm(Region form, Stage parent, String css) {
        double width = 800;
        double height = 600;
        if (form.getPrefWidth() > 0) {
            width = form.getPrefWidth();
        }
        if (form.getPrefHeight() > 0) {
            height = form.getPrefHeight();
        }

        AtomicReference<Double> xOffset = new AtomicReference<>((double) 0);
        AtomicReference<Double> yOffset = new AtomicReference<>((double) 0);
        form.setOnMousePressed(event -> {
            xOffset.set(event.getSceneX());
            yOffset.set(event.getSceneY());
        });

        form.setOnMouseDragged(event -> {
            mainStage.setX(event.getScreenX() - xOffset.get());
            mainStage.setY(event.getScreenY() - yOffset.get());
        });


        mainPane.getChildren().add(form);

        Scene scene = new Scene(mainPane, width, height);
        mainPane.prefWidthProperty().bind(scene.widthProperty());
        mainPane.prefHeightProperty().bind(scene.heightProperty());
        if (StrUtil.isNotEmpty(css)) {
            scene.getStylesheets().add(css);
        }
        mainStage.setScene(scene);
        if (mainStage.getUserData() == null) {
            mainStage.setResizable(false);
            mainStage.initStyle(StageStyle.UNDECORATED);
            if (parent != null) {
                mainStage.initOwner(parent);
            }
            mainStage.setUserData("ok");
        }
        mainStage.show();
    }

    public static void setStageBackground(String path) {
        ImageView bgImageView = new ImageView(new Image(path));
        Image frostImage = bgImageView.snapshot(new SnapshotParameters(), null);
        ImageView frost = new ImageView(frostImage);
        Pane frostPane = new Pane(frost);
        frostPane.setEffect(new BoxBlur(60, 60, 10));
        StackPane frostView = new StackPane(frostPane);
        mainPane.getChildren().add(0, frostView);
    }


    public static void showWindow(Region form) {

    }

    public static void showMessage(Region messageRegion) {
        mainPane.getChildren().add(messageRegion);
        messageList.add(messageRegion);
        messageRegion.setLayoutX(mainPane.getPrefWidth()/2-messageRegion.getPrefWidth()/2);
        messageRegion.setLayoutY(-messageRegion.getPrefHeight());

        //创建时间轴
        final Timeline timeline=new Timeline();

        //将x的位置在500ms内移动到300处
        final KeyValue kv=new KeyValue(messageRegion.layoutYProperty(), messageRegion.getPrefHeight()+10);
        final KeyFrame kf=new KeyFrame(Duration.millis(500), kv);
        //将关键帧加到时间轴中
        timeline.getKeyFrames().add(kf);
        timeline.play();//运行


    }

    public static void showMessage(Region messageRegion, Integer second) {

    }


    public static void showForm(Region form) {
        showForm(form, null, "");
    }


}
